import { launchOpen } from '../../api/manage'
import create from 'dd-store'
import pagesStore from '../../stores/globalStore'


create.Page({
  store: pagesStore,
  data: {
    min: new Date('2019/01/15'),
    max: new Date('2023/08/20'),
    periodList: [
      { label: '月度', value: '0025' },
    ],
    defaultValue: '0025',
    result: [
      { label: '考核结束后自动公开', value: '1' },
      { label: '管理员公示结果', value: '2' }
    ],
    dataList: [],
    quarter: [],
    code: '0025',
    count: '',
    checkName: '',
    perch: [],
    groups: [],
    year: '',
    evaluationFormText: '请选择',
    launchShowList: []
  },
  onLoad(options) {
    console.log(options.launch)
    this.setData({
      periodList: JSON.parse(options.launch),
      perch: options.launch
    })

    this.getdata()
  },
  routeForm() {
    if (this.data.code !== '') {
      dd.navigateTo({
        url: '/pages/manage/checkform/checkform?code=' + this.data.code + '&launch=' + this.data.perch
      })
    }
  },
  launch() {
    dd.getStorage({
      key: 'currentCity',
      success: (res) => {
        let data = {
          startUserDDId: res.data.value.userId,
          pmGroupIds: this.data.groups,
          pmFormName: this.data.checkName,
          datePeriodTypeCode: this.data.code,
          dateyear: this.data.year,
          dateCount: this.data.count
        }
        if (this.data.groups.length == 0) {
          this.modal('请选择考评表')
        } else if (this.data.checkName == '') {
          this.modal('请选择对应的周期')
        } else {
          launchOpen(JSON.stringify(data)).then(rs => {
            if (rs.result == 'success') {
              this.modal(rs.msg)
            }
          })
        }
      },
      fail: function (res) {
        dd.alert({ content: '请稍后再试' });
      }
    });


  },
  handleOk(value, format,) {
    if (value != this.data.code) {
      this.$store.data.launchShowList = []
      this.$store.data.evaluationFormText = '请选择'
      this.update()
    }
    // console.log(value);
    this.setData({
      code: value,
      checkName: '',
      count: ''
    })
  },
  handleDismiss(e) {
  },
  handleFormatLabel(type, value) {
    return String(value);
  },
  getdata() {
    // 获取当前时间
    var now = new Date();
    // 获取年份
    var year = now.getFullYear();
    this.setData({
      year
    })
    // 获取月份（注意月份从0开始，所以需要加1）
    var month = now.getMonth() + 1;
    // 获取日期
    var day = now.getDate();
    let list = []
    let newList = list.push({ label: year + '年上半年', value: '1' }, { label: year + '年下半年', value: '2' })
    let query = []
    let Lists = query.push({ label: year + '-Q1', value: year + '年第1季度绩效考核', index: '1' }, {
      label: year + '-Q2', value: year + '年第2季度绩效考核', index: '2'
    }, { label: year + '-Q3', value: year + '年第3季度绩效考核', index: '3' }, { label: year + '-Q4', value: year + '年第4季度绩效考核', index: '4' })
    this.setData({
      dataList: list,
      quarter: query
    })
  },
  handleyearOk(date, format) {
    if (format.length > 4) {
      let demo = format.split('-')[1]
      let reg = RegExp(/^0/);
      this.setData({
        checkName: format.split('-').join('年') + '月绩效考核',
        count: reg.test(demo) ? format.split('-')[1].split('0').join('') : format.split('-')[1]
      })
    } else {
      this.setData({
        checkName: format + '年绩效考核',
        count: ''
      })
    }
  },
  nomalOk(value, selectedOption, e) {
    if (value.length > 10) {
      this.setData({
        checkName: value,
        count: selectedOption.index
      })
    } else {
      this.setData({
        checkName: selectedOption.label + '绩效考核',
        count: value
      })
    }
  },
  onChange(value,) {
    this.setData({
      checkName: value
    })
  },
  modal(content) {
    dd.alert({
      title: '',
      content,
      buttonText: '我知道了',
      success() {
        console.log('用户点击了「我知道了」');
      },
    })
    // if(content == )
  },
  changeData(value) {
    this.setData({
      groups: value
    })
  }

});
